/*
 * BackupWriterManagerDialog.java
 *
 * Created on December 24, 2007, 3:13 AM
 */

package backupit.gui;


import backupit.core.FileInfo;
import backupit.core.dirbackup.DirBackupInfo;
import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

class MyCellRenderer extends JLabel implements ListCellRenderer {
     ImageIcon longIcon = new ImageIcon(getClass().getResource("/backupit/gui/pixmaps/xarchive_32x32.png"));
     ImageIcon shortIcon = new ImageIcon(getClass().getResource("/backupit/gui/pixmaps/xarchive_32x32.png"));

     // This is the only method defined by ListCellRenderer.
     // We just reconfigure the JLabel each time we're called.

     public Component getListCellRendererComponent(
       JList list,              // the list
       Object value,            // value to display
       int index,               // cell index
       boolean isSelected,      // is the cell selected
       boolean cellHasFocus)    // does the cell have focus
     {
         if(value==null) return this;
         String s = ((DirBackupInfo)value).getBackup().getName();
         setText(s);
         setIcon((s.length() > 10) ? longIcon : shortIcon);
         if (isSelected) {
             setBackground(list.getSelectionBackground());
             setForeground(list.getSelectionForeground());
         } else {
             setBackground(list.getBackground());
             setForeground(list.getForeground());
         }
         setEnabled(list.isEnabled());
         setFont(list.getFont());
         setOpaque(true);
         return this;
     }
 }

/**
 *
 * @author  dbotelho
 */
public class BackupWriterManagerDialog extends javax.swing.JDialog {

    /** Creates new form BackupWriterManagerDialog */
    public BackupWriterManagerDialog(javax.swing.JFrame frame, boolean modal) {
        super(frame, modal);
        initComponents();
    }
    
    public void updateUI(String looknFeel) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
        
        this.repaint();//UIManager.setLookAndFeel(looknFeel);
    }

    public void addBackupInfo(DirBackupInfo backupInfo){
        ((DefaultListModel)jList1.getModel()).addElement(backupInfo);
        //backupInfoTabbedPane.addTab(backupInfo.getBackup().getName(), new ViewBackupInfoPanel(backupInfo));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        fileCountTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        totalBytesTextField = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        reportSetTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Backup Manager");

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setAutoscrolls(true);

        jList1.setModel(new javax.swing.DefaultListModel());
        jList1.setCellRenderer(new MyCellRenderer());
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseListener(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jSplitPane1.setTopComponent(jScrollPane1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Backup Info"));

        jLabel1.setText("Number of files copied:");

        fileCountTextField.setEditable(false);
        fileCountTextField.setText("0");

        jLabel2.setText("Number of bytes copied:");

        totalBytesTextField.setEditable(false);
        totalBytesTextField.setText("0");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(totalBytesTextField)
                    .addComponent(fileCountTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fileCountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(totalBytesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        reportSetTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Path", "Name", "Size"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(reportSetTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mouseListener(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mouseListener
        if (evt.getClickCount() == 2) {
            int index = jList1.locationToIndex(evt.getPoint());
            System.out.println("Double clicked on Item " + index);
            setBackupInfo( (DirBackupInfo)jList1.getSelectedValue());
        }
    }//GEN-LAST:event_mouseListener
    protected void setBackupInfo(DirBackupInfo backupInfo){

        System.out.println(backupInfo.getBackup().getName());
        //this.setTitle(this.backupInfo.getBackup().getName());
        loadBackupInfo(backupInfo);
        loadTableFiles(backupInfo);
        //this.repaint();//this.getGraphics()
    }
    private void loadBackupInfo(DirBackupInfo backupInfo) {
        this.fileCountTextField.setText(backupInfo.getWrittenFiles().size()+"");
        long numBytes=0;
        
        for(FileInfo file: backupInfo.getWrittenFiles()){
            numBytes+=file.getLength();
        }
        this.totalBytesTextField.setText(numBytes+"");
              
    }
    // End of variables declaration

    private void clearAll(DefaultTableModel model){
        model.getDataVector().clear();
        reportSetTable.repaint();
    }
    private void loadTableFiles(DirBackupInfo backupInfo) {
        
        DefaultTableModel model = (DefaultTableModel) reportSetTable.getModel();
        clearAll(model);
        //clearTableModel(model);
        for(FileInfo file: backupInfo.getWrittenFiles())
            model.insertRow(model.getRowCount(), 
            new Object[]{file.getRelativePath(),file.getName(),file.getLength()});
    }
    /*
     * @param args the command line arguments
     *
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                BackupWriterManagerDialog dialog = new BackupWriterManagerDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField fileCountTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable reportSetTable;
    private javax.swing.JTextField totalBytesTextField;
    // End of variables declaration//GEN-END:variables
    
}
